package com.jkoss.wine.system.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.util.CryptoUtils;
import com.jkoss.wine.system.entity.Loginlog;
import com.jkoss.wine.system.entity.Permission;
import com.jkoss.wine.system.service.ICommonlyService;
import com.jkoss.wine.system.service.ILoginlogService;
import com.jkoss.wine.system.service.IMessageNotificationService;

/**
 * 主页 前端控制器
 * 
 * @Author chair
 * @Version 1.0, 2018年9月23日
 * @See
 * @Since com.jkoss.mengal.system.controller
 * @Description: TODO
 */
@Controller()
@RequestMapping("/")
public class IndexController extends BaseController {

	@Autowired
	private ILoginlogService iLoginlogService;
	@Autowired
	private ICommonlyService iCommonlyService;
	@Autowired
	private IMessageNotificationService iMessageNotificationService;

	/**
	 * 去主页
	 * 
	 * @return
	 */
	@RequestMapping("/")
	public String index(ModelMap map) {
		return "index";
	}

	/**
	 * 去登录
	 * 
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLoing() {
		if (isAjax(getRequest())) {
			return "forward:/toTimeOut";
		}
		return "login";
	}

	/**
	 * 登录逻辑
	 * 
	 * @param lgnName 登录用户
	 * @param pwd     密码
	 * @param map
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public String login(String lgnName, String pwd, ModelMap map, HttpSession session) throws Exception {
		// 构造登录参数
		UsernamePasswordToken token = new UsernamePasswordToken(lgnName, CryptoUtils.encodeMD5(pwd));
		try {
			// 交给Realm类处理
			SecurityUtils.getSubject().login(token);

			Loginlog loginlog = new Loginlog();
			// 是否删除：0-未删除、1-已删除、2-不能删除
			loginlog.setDel(0);
			// 添加时间
			loginlog.setAddTime(CommonUtil.getDate());
			// 当操作人为null时，表示自动录入
//			loginlog.setOperator(getUserId());
			// 登录时间
			loginlog.setLoginTime(CommonUtil.getDate());
			// 登录人
			loginlog.setEmployeeId(getUserId());
			// 登录ip
			loginlog.setIp(getAddr());
			// 登录次数
			Integer MaxFrequency = iLoginlogService.getMaxFrequency();
			if (MaxFrequency == null) {
				MaxFrequency = 0;
			}
			MaxFrequency += 1;
			loginlog.setFrequency(MaxFrequency);
			iLoginlogService.insert(loginlog);

		} catch (UnknownAccountException uae) {
			map.put("msg", "未知用户");
			return "forward:/toLogin";
		} catch (IncorrectCredentialsException ice) {
			map.put("msg", "密码错误");
			return "forward:/toLogin";
		} catch (DisabledAccountException ae) {
			map.put("msg", "帐号停用");
			return "forward:/toLogin";
		} catch (AuthenticationException ae) {
			// unexpected condition? error?
			map.put("msg", "服务器繁忙");
			return "forward:/toLogin";
		}
		return "redirect:/";
	}

	/**
	 * 去登出
	 * 
	 * @return
	 */
	@RequestMapping("/toLogout")
	public String toLogout() {
		return "logoutConfirm";
	}

	/**
	 * 登出逻辑
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute(Constant.SESSION_MENU_KEY);
		session.removeAttribute(Constant.SESSION_USER_KEY);
		session.removeAttribute(Constant.SESSION_URLS_KEY);
		SecurityUtils.getSecurityManager().logout(SecurityUtils.getSubject());
		return "login";
	}

	/**
	 * 超时逻辑
	 * 
	 * @return
	 */
	@RequestMapping("/toTimeOut")
	public String toTimeOut() {
		return "timeoutConfirm";
	}

	/**
	 * 没有权限
	 * 
	 * @return
	 */
	@RequestMapping("/toNoAuth")
	public String toNoAuth() {
		return "noAuthConfirm";
	}

}
